import { personalDetails } from "./initialState";
import { createWithEqualityFn } from "zustand/traditional";

export type PersonalDetailState = { personalDetails: PersonalDetails };

export type PersonalDetailActions = {
  update: (content: any) => void;
  add?: (content: any) => void;
  delete?: (content: any) => void;
  updateDetailOrder: (data: (keyof PersonalDetails)[]) => void;
  updateDetail: (data: PersonalDetails) => void;
};

export type PersonalDetailStore = PersonalDetailState & PersonalDetailActions;

export const defaultInitState = personalDetails;

export const createPersonalDetailStore = (initState = defaultInitState) => {
  return createWithEqualityFn<PersonalDetailStore>()((set) => ({
    personalDetails: initState,
    update: (data: any) => {
      set(() => ({ personalDetails: data }));
    },
    updateDetail: (data) => {
      set((state) => ({
        personalDetails: {
          ...state.personalDetails,
          ...data,
        },
      }));
    },
    updateDetailOrder: (data) => {
      set((state) => ({
        personalDetails: {
          ...state.personalDetails,
          detailsOrder: data,
        },
      }));
    },
  }));
};
